package com.vitalfit.controller;


import com.vitalfit.annotation.LoginUser;
import com.vitalfit.enums.ResultEnum;
import com.vitalfit.model.dto.AppointmentDTO;
import com.vitalfit.model.dto.FitnessPlanDTO;
import com.vitalfit.model.entity.fitness.Appointment;
import com.vitalfit.model.vo.AppointmentVO;
import com.vitalfit.model.vo.FitnessPlanVO;
import com.vitalfit.model.vo.ResultVO;
import com.vitalfit.model.vo.UserAuthentication;
import com.vitalfit.service.serviceImpl.AppointmentServiceImpl;
import com.vitalfit.utils.ResultVOUtil;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/appointment")
@RequiredArgsConstructor
@Validated
public class AppointmentController {

    private final AppointmentServiceImpl appointmentServiceImpl;

    @PreAuthorize("hasRole('ROLE_CUSTOMER')")
    @PostMapping
    public ResultVO<AppointmentVO> makeAppointment(@LoginUser UserAuthentication userAuthentication, @RequestBody @Valid AppointmentDTO appointmentDTO) {
        // 预约教练路由
        return ResultVOUtil.buildResult(ResultEnum.ORDER_SUCCESS,
                appointmentServiceImpl
                        .makeAppointment(userAuthentication.getToken(), userAuthentication.getUserId(), appointmentDTO));
    }

    @PreAuthorize("hasRole('ROLE_CUSTOMER')")
    @PostMapping("/fitness_plan")
    public ResultVO<FitnessPlanVO> purchaseFitnessPlan(@LoginUser UserAuthentication userAuthentication, @RequestBody @Valid FitnessPlanDTO fitnessPlanDTO) {
        // 购买训练计划路由
        return ResultVOUtil.buildResult(ResultEnum.PURCHASE_SUCCESS,
                appointmentServiceImpl
                        .purchaseFitnessPlan(userAuthentication.getToken(),userAuthentication.getUserId(), fitnessPlanDTO));
    }

    @PreAuthorize("hasRole('ROLE_CUSTOMER')")
    @PostMapping("/customer/user")
    public ResultVO<List<Appointment>> getUserAppointments(
            @LoginUser UserAuthentication userAuthentication,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {

        return ResultVOUtil.buildResult(
                ResultEnum.GET_SUCCESS, appointmentServiceImpl.getUserAppointmentList
                (userAuthentication.getToken(), userAuthentication.getUserId(), page,size).getContent());
    }
    @PreAuthorize("hasRole('ROLE_SHOPMANAGER')")
    @PostMapping("/customer/all")
    public ResultVO<List<Appointment>> getAllAppointments(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return ResultVOUtil.buildResult(
                ResultEnum.GET_SUCCESS, appointmentServiceImpl.getAllAppointmentList(page,size).getContent());

    }

}


